package main

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/gctx"
	_ "github.com/gogf/gf/contrib/drivers/sqlite/v2"

	"goldtree/internal"
)

func main() {
	ctx := gctx.GetInitCtx()

	// 初始化数据库
	if err := internal.InitDB(ctx); err != nil {
		g.Log().Fatal(ctx, "Failed to initialize database:", err)
		return
	}

	// 插入初始数据
	if err := insertInitialData(ctx); err != nil {
		g.Log().Fatal(ctx, "Failed to insert initial data:", err)
		return
	}

	g.Log().Info(ctx, "Database migration completed successfully!")
}

func insertInitialData(ctx context.Context) error {
	db := g.DB()

	// 插入默认标签分类
	defaultTags := []g.Map{
		{
			"name":        "大盘股",
			"category":    "market_cap",
			"description": "市值大于1000亿的股票",
			"color":       "#52c41a",
		},
		{
			"name":        "中盘股",
			"category":    "market_cap", 
			"description": "市值在100-1000亿之间的股票",
			"color":       "#1890ff",
		},
		{
			"name":        "小盘股",
			"category":    "market_cap",
			"description": "市值小于100亿的股票",
			"color":       "#fa8c16",
		},
		{
			"name":        "金融",
			"category":    "industry",
			"description": "银行、保险、证券等金融行业",
			"color":       "#722ed1",
		},
		{
			"name":        "科技",
			"category":    "industry",
			"description": "软件、硬件、通信等科技行业",
			"color":       "#13c2c2",
		},
		{
			"name":        "消费",
			"category":    "industry",
			"description": "食品饮料、服装等消费行业",
			"color":       "#eb2f96",
		},
		{
			"name":        "新能源",
			"category":    "concept",
			"description": "新能源汽车、光伏、风电等概念",
			"color":       "#52c41a",
		},
		{
			"name":        "人工智能",
			"category":    "concept",
			"description": "AI、机器学习相关概念",
			"color":       "#2f54eb",
		},
		{
			"name":        "高风险",
			"category":    "risk",
			"description": "波动较大的高风险股票",
			"color":       "#f5222d",
		},
		{
			"name":        "稳健型",
			"category":    "risk",
			"description": "波动较小的稳健型股票",
			"color":       "#52c41a",
		},
	}

	for _, tag := range defaultTags {
		// 检查标签是否已存在
		count, err := db.Model("tags").Where("name=? AND category=?", tag["name"], tag["category"]).Count()
		if err != nil {
			return err
		}
		if count == 0 {
			if _, err := db.Model("tags").Insert(tag); err != nil {
				g.Log().Warning(ctx, "Failed to insert tag:", tag["name"], err)
			}
		}
	}

	// 插入默认股票池
	defaultPools := []g.Map{
		{
			"name":        "沪深300成分股",
			"description": "沪深300指数成分股票池",
			"type":        "static",
			"max_size":    300,
		},
		{
			"name":        "自选股",
			"description": "用户自选股票池",
			"type":        "static",
			"max_size":    50,
		},
		{
			"name":        "高成长股池",
			"description": "基于成长性指标的动态股票池",
			"type":        "dynamic",
			"max_size":    100,
		},
		{
			"name":        "价值股池",
			"description": "基于价值指标的动态股票池", 
			"type":        "dynamic",
			"max_size":    100,
		},
	}

	for _, pool := range defaultPools {
		// 检查股票池是否已存在
		count, err := db.Model("stock_pools").Where("name=?", pool["name"]).Count()
		if err != nil {
			return err
		}
		if count == 0 {
			if _, err := db.Model("stock_pools").Insert(pool); err != nil {
				g.Log().Warning(ctx, "Failed to insert stock pool:", pool["name"], err)
			}
		}
	}

	// 插入一些示例股票数据
	sampleStocks := []g.Map{
		{
			"code":         "000001",
			"name":         "平安银行",
			"market":       "SZ",
			"industry":     "银行",
			"sector":       "金融",
			"listing_date": "1991-04-03",
		},
		{
			"code":         "000002",
			"name":         "万科A",
			"market":       "SZ", 
			"industry":     "房地产",
			"sector":       "房地产",
			"listing_date": "1991-01-29",
		},
		{
			"code":         "600000",
			"name":         "浦发银行",
			"market":       "SH",
			"industry":     "银行",
			"sector":       "金融",
			"listing_date": "1999-11-10",
		},
		{
			"code":         "600036",
			"name":         "招商银行",
			"market":       "SH",
			"industry":     "银行", 
			"sector":       "金融",
			"listing_date": "2002-04-09",
		},
		{
			"code":         "000858",
			"name":         "五粮液",
			"market":       "SZ",
			"industry":     "食品饮料",
			"sector":       "消费",
			"listing_date": "1998-04-27",
		},
	}

	for _, stock := range sampleStocks {
		// 检查股票是否已存在
		count, err := db.Model("stocks").Where("code=?", stock["code"]).Count()
		if err != nil {
			return err
		}
		if count == 0 {
			if _, err := db.Model("stocks").Insert(stock); err != nil {
				g.Log().Warning(ctx, "Failed to insert stock:", stock["code"], err)
			}
		}
	}

	// 插入系统配置
	defaultConfigs := []g.Map{
		{
			"config_key":   "system.version",
			"config_value": "1.0.0",
			"config_type":  "string",
			"description":  "系统版本号",
		},
		{
			"config_key":   "trading.enabled",
			"config_value": "false",
			"config_type":  "bool",
			"description":  "是否启用交易功能",
		},
		{
			"config_key":   "data.update_interval",
			"config_value": "60",
			"config_type":  "int",
			"description":  "数据更新间隔(秒)",
		},
	}

	for _, config := range defaultConfigs {
		// 检查配置是否已存在
		count, err := db.Model("system_config").Where("config_key=?", config["config_key"]).Count()
		if err != nil {
			return err
		}
		if count == 0 {
			if _, err := db.Model("system_config").Insert(config); err != nil {
				g.Log().Warning(ctx, "Failed to insert config:", config["config_key"], err)
			}
		}
	}

	g.Log().Info(ctx, "Initial data inserted successfully")
	return nil
}